package com.woniu.config;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 自定义security密码校验方式，使用明文加密
 * @author tarnett
 */
@Component
public class CustomPasswordEncoder implements PasswordEncoder {

    @Override
    public String encode(CharSequence charSequence) {
        return charSequence.toString();
    }

    /**
     *
     * @param charSequence      用户输入的密码
     * @param dbPassword        数据库查询出来的密码
     * @return
     */
    @Override
    public boolean matches(CharSequence charSequence, String dbPassword) {
        // 用户输入的密码为空，则表示授权登录，不进行密码的判断
        if(StringUtils.isEmpty(charSequence.toString())){
            return true;
        }
        // 用户输入的密码不为空，则表示正常登录，进行密码校验
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder.matches(charSequence, dbPassword);
    }
}
